package com.szholly.pro.watergas_server.ps.android.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import com.szholly.data.general.RowBase;
import com.szholly.pro.watergas_server.ps.android.common.DateTime;
import com.szholly.pro.watergas_server.ps.android.common.ResultUtils;
import com.szholly.pro.watergas_server.ps.android.common.ValidUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.ServletContextAware;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 污水接口控制器
 * 
 * 1.getRuntimeData 获取实时数据
 * 
 * 2.getHistoryDataInitData 获取历史数据初始化
 *  
 * @author joey
 *
 */
@Controller
@RequestMapping("/android/sewage/*")
public class SewageController implements ServletContextAware {
	@SuppressWarnings("unused")
	private ServletContext servletContext;
	private static SewageService mSewageManager;	
	
	@Override
	public void setServletContext(ServletContext arg0) {
		this.servletContext = arg0;
	}

	@RequestMapping("/getRuntimeData")
	@ResponseBody
	public String getRuntimeData(String token, String factoryId,String type) {
		try{
			ValidUtils.validateToken(token);
			ValidUtils.hasText(factoryId,"工厂编号不能为空");	
			if(null != type && type.equals("surface")){
				return getSewageManager().getSurfaceRuntimeData(factoryId);
			}else{
				return getSewageManager().getRuntimeData(factoryId);
			}
			
		}catch (Exception e) {
			return ResultUtils.getToastResult(e);
		}
	}

	@RequestMapping("/getHistoryDataInitData")
	@ResponseBody
	public String getHistoryData(String token, String factoryId) {
		try{
			ValidUtils.validateToken(token);
			ValidUtils.hasText(factoryId,"工厂编号不能为空");	
			return getSewageManager().getHistoryDataInitData(factoryId);
		}catch (Exception e) {
			return ResultUtils.getToastResult(e);
		}
	}
	
	@RequestMapping("/getWarnData")
	@ResponseBody
	public String getWarnData(String token, String userId,String unit,String stationCode, String isRecover,String startTime,String endTime,String pageIndex) {
		try{
			ValidUtils.validateToken(token);
			ValidUtils.hasText(startTime,"起始时间为空，获取报警信息失败");
			ValidUtils.hasText(endTime,"结束时间为空，获取报警信息失败");
			int index = Integer.parseInt(pageIndex);
			ValidUtils.isTrue(index>0,"页码有误,获取报警信息失败");	
			Map<String, Object> map = getSewageManager().getWarnList(userId,unit,stationCode,isRecover,startTime, endTime,index);
			return ResultUtils.getToastResult(map);
		}catch (Exception e) {
			return ResultUtils.getToastResult(e);
		}
	}
	
	@RequestMapping("/getOneDayHourData")
	@ResponseBody
	public String getOneDayHourData(String token, String stationCode,String dateTime) {
		try{
			ValidUtils.hasText(stationCode, "站点编号为空，无法获取一天的小时数据");
			ValidUtils.hasText(dateTime, "日期为空，无法获取一天的小时数据");
			DateTime parse = DateTime.parse(dateTime, "yyyy-MM-dd HH:mm:ss");
			Map<String,Object>resultMap = new HashMap<>();
			List<RowBase> valueList = getSewageManager().getOneDayHourData(stationCode, parse);
			List<RowBase> alarmLimitList = getSewageManager().getAlarmLimit(stationCode);
			resultMap.put("valueList", valueList);
			resultMap.put("alarmLimitList", alarmLimitList);
			return ResultUtils.getToastResult(resultMap);
		}catch (Exception e) {
			return ResultUtils.getToastResult(e);
		}
	}
	
	// 获取历史分钟数据
	@RequestMapping("/getOneDayMinData")
	@ResponseBody
	public String getOneDayMinData(String token, String stationCode,String dateTime) {
		try{
			ValidUtils.hasText(stationCode, "站点编号为空，无法获取一天的小时数据");
			ValidUtils.hasText(dateTime, "日期为空，无法获取一天的小时数据");
			DateTime parse = DateTime.parse(dateTime, "yyyy-MM-dd HH:mm:ss");
			Map<String,Object>resultMap = new HashMap<>();
			List<RowBase> valueList = getSewageManager().getOneDayMinData(stationCode, parse);
			List<RowBase> alarmLimitList = getSewageManager().getAlarmLimit(stationCode);
			resultMap.put("valueList", valueList);
			resultMap.put("alarmLimitList", alarmLimitList);
			return ResultUtils.getToastResult(resultMap);
		}catch (Exception e) {
			return ResultUtils.getToastResult(e);
		}
	}
	
	// 获取历史分钟数据
	@RequestMapping("/getWarnStateDes")
	@ResponseBody
	public String getWarnStateDes(String token, String stationCode) {
		try{
			ValidUtils.hasText(stationCode,"站点编号为空,报警状态详情获取失败");
			List<String> warnStateDes = getSewageManager().getWarnStateDes(stationCode);			
			return ResultUtils.getToastResult(warnStateDes);
		}catch (Exception e) {
			return ResultUtils.getToastResult(e);
		}
	}

	public static SewageService getSewageManager() {
		if (mSewageManager == null) {
			mSewageManager = new SewageService();
		}
		return mSewageManager;
	}
}
